/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author !gaso
 */
class ButtonEditor extends DefaultCellEditor {

        protected JButton button;
        private String label;
        private boolean isPushed;
        private JTable dev_tabla;
        //Codigo del Elemento a Consultar/Modificar/Eliminar
        private String id, Action;

        public ButtonEditor(JCheckBox checkBox, String ed_img, JTable tablaImplementacion) {
            super(checkBox);
            button = new JButton();
            dev_tabla = tablaImplementacion;
            //Agregar Imagen al Boton al dar Clic
            button.setIcon(new javax.swing.ImageIcon(getClass().getResource(ed_img)));
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    fireEditingStopped();
                       

                }
            });
        }

    

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            if (isSelected) {
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } else {
                button.setForeground(table.getForeground());
                button.setBackground(table.getBackground());
            }
            label = (value == null) ? "" : value.toString();
            button.setText(label);
            isPushed = true;


            return button;
        }
        

        public Object getCellEditorValue() {
                
            if (isPushed) {

                //  JOptionPane.showMessageDialog(button, label + String.valueOf(ed_dev_tabla.getValueAt(ed_dev_tabla.getEditingRow(), 0)));


                id = String.valueOf(dev_tabla.getValueAt(dev_tabla.getEditingRow(), 0));
                Action = label;
                System.out.println(Action + " " + id);
                if (Action == "Consultar") {

                        System.out.println(Action + " " + id);
                } else if (Action == "Modificar") {

                  

                } else if (Action.equals("Eliminar")) {
                    
                    String ed_nombreCliente = String.valueOf(dev_tabla.getValueAt(dev_tabla.getEditingRow(), 2)) + " " + String.valueOf(dev_tabla.getValueAt(dev_tabla.getEditingRow(), 3));
                    //  JOptionPane.showConfirmDialog(null,"Esta Seguro que Quiere Eliminar al Proveedor"+ed_nombreProveedor);
                    int opcion = JOptionPane.showConfirmDialog(null, "Confirma la Eliminacion de este Cliente " + ed_nombreCliente + "", "Eliminado Cliente", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
                    if (opcion == 0) {
//                        if (clientes.eliminarCliente(id)) {
//                            /*
//                             * Despues de una eliminacion exitosa
//                             * se agregan los nuevos valores
//                             * a la Base de Datos.
//                             */
//                            JOptionPane.showMessageDialog(null, "El Cliente ha sido Eliminado Correctamente.", "Eliminacion Exitosa", JOptionPane.INFORMATION_MESSAGE);
//                            setModelodev_tabla(clientes.ed_llenardev_tablaClientes("ASC"), false);
//
//                        } else {
//                            System.out.println("Error! No se puedo eliminar Proveedor");
//                        }

                    } else {
                    }

                }

            }
            isPushed = false;
            return new String(label);
        }

        public boolean stopCellEditing() {
            isPushed = false;
            return super.stopCellEditing();
        }

        protected void fireEditingStopped() {
            try {
                super.fireEditingStopped();
            } catch (Exception e) {
                System.out.println("Han Cambiado los Datos en la dev_tabla " + e.getMessage());
            }
        }
    }
